package cc.iotkit.manager.controller;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Request;
import cc.iotkit.data.dao.AreaMappingRepository;
import cc.iotkit.data.model.TbAreaMapping;
import cc.iotkit.model.device.DeviceInfoArea;
import cc.iotkit.model.response.ResponseResult;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Title: cc.iotkit.manager.controller
 * @author: Sunne
 * @date: 2024-09-24 15:55
 * @Description: 代码备注说明
 */
@Api(tags = {"区域控制器"})
@Slf4j
@RestController
@RequestMapping("/areamp")
public class AreaMappingController {
    @Autowired
    private AreaMappingRepository areaMappingRepository;

    @ApiOperation("增加一个设备区域关系")
    @SaCheckPermission("iot:area:add")
    @PostMapping("/addMapping")
    public ResponseResult<TbAreaMapping> getAreaMappingRepository(@Validated @RequestBody Request<TbAreaMapping> areaMapping) {
        areaMapping.getData().setId(UUID.randomUUID().getMostSignificantBits());
        return ResponseResult.okHasData(areaMappingRepository.saveAndFlush(areaMapping.getData()));
    }
    @ApiOperation("查询当前区域所有设备")
    @SaCheckPermission("iot:area:query")
    @PostMapping("/listMapping")
    public Page<DeviceInfoArea> getAreaMappingRepository(@Validated @RequestBody PageRequest<TbAreaMapping> request ) {
        return areaMappingRepository.findAllByAreaId(Long.parseLong(request.getData().getAreaId()), Pageable.ofSize(request.getPageSize()).withPage(request.getPageNum()-1));
    }
}